/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.viewfun;


import me.panpf.sketch.ImageView;
import me.panpf.sketch.zoom.ImageZoomer;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.multimodalinput.event.TouchEvent;

/**
 * {@link Image} 缩放功能
 */
@SuppressWarnings("WeakerAccess")
public class ImageZoomFunction extends ViewFunction {

    private ImageZoomer zoomer;

    public ImageZoomFunction(FunctionPropertyView view) {
        this.zoomer = new ImageZoomer(view);
    }

    @Override
    public void onAttachedToWindow() {
        zoomer.reset("onAttachedToWindow");
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        zoomer.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(TouchEvent event) {
        return zoomer.onTouchEvent(event);
    }

    @Override
    public boolean onDrawableChanged(String callPosition, Element oldDrawable, Element newDrawable) {
        zoomer.reset("onDrawableChanged");
        return false;
    }

    @Override
    public void onSizeChanged(int left, int top, int right, int bottom) {
        zoomer.reset("onSizeChanged");
    }

    @Override
    public boolean onDetachedFromWindow() {
        recycle("onDetachedFromWindow");
        return false;
    }

    void recycle(String why) {
        zoomer.recycle(why);
    }

    public Image.ScaleMode getScaleType() {
        Image.ScaleMode imageScaleType = Image.ScaleMode.CENTER;
        switch (zoomer.getScaleType()) {
            case INSIDE:
                imageScaleType =Image.ScaleMode.INSIDE;
                break;
            case STRETCH:
                imageScaleType = Image.ScaleMode.STRETCH;
                break;
            case ZOOM_END:
                imageScaleType = Image.ScaleMode.ZOOM_END;
                break;
            case ZOOM_START:
                imageScaleType = Image.ScaleMode.ZOOM_START;
                break;
            case CLIP_CENTER:
                imageScaleType = Image.ScaleMode.CLIP_CENTER;
                break;
            case ZOOM_CENTER:
                imageScaleType = Image.ScaleMode.ZOOM_CENTER;
                break;
            case CENTER:
            default:
                imageScaleType = Image.ScaleMode.CENTER;
                break;
        }
        return imageScaleType;
    }

    public void setScaleType(Image.ScaleMode scaleType) {
        ImageView.ScaleType imageScaleType = ImageView.ScaleType.CENTER;
        switch (scaleType) {
            case INSIDE:
                imageScaleType = ImageView.ScaleType.INSIDE;
                break;
            case STRETCH:
                imageScaleType = ImageView.ScaleType.STRETCH;
                break;
            case ZOOM_END:
                imageScaleType = ImageView.ScaleType.ZOOM_END;
                break;
            case ZOOM_START:
                imageScaleType = ImageView.ScaleType.ZOOM_START;
                break;
            case CLIP_CENTER:
                imageScaleType = ImageView.ScaleType.CLIP_CENTER;
                break;
            case ZOOM_CENTER:
                imageScaleType = ImageView.ScaleType.ZOOM_CENTER;
                break;
            case CENTER:
            default:
                imageScaleType = ImageView.ScaleType.CENTER;
                break;
        }
        zoomer.setScaleType(imageScaleType);
    }

    public ImageZoomer getZoomer() {
        return zoomer;
    }
}
